<template>
    <view class="globe_back">
        <view class="back_fff br_16 p_32 addressBox" v-for="item in list"  v-if="type!=1">
            <view>
                <view class="f_28">
                    {{ item.real_name }}
                    <text class="cl_999CA3 ml_16">{{ item.phone }}</text>
                </view>
                <view class="f_28">{{ item.province }}{{ item.city }}{{ item.district }}{{ item.detail }}</view>
            </view>
            <view class="flex justify_content_between mt_74">
                <view>
                    <u-radio-group v-model="defaultItem">
                        <u-radio :customStyle="{ marginBottom: '8px' }" value="设为默认" :disabled="false" label="设为默认" :name="item.id" @change="radioChange"></u-radio>
                        <!-- <u-radio :customStyle="{ marginBottom: '8px' }" v-for="(item, index) in radiolist1" :key="index" :label="item.name" :name="item.name" @change="radioChange"> </u-radio> -->
                    </u-radio-group>
                </view>
                <view class="flex">
                    <view class="mr_24" @click="edit(item)">
                        <image class="addicon img_32" src="@/static/imgs/icon_bj.png" mode=""></image>
                        <text class="f_28 cl_999CA3">编辑</text>
                    </view>
                    <view @click="handleRemoveAddress(item.id)">
                        <image class="addicon img_32" src="@/static/imgs/icon_sc.png" mode=""></image>
                        <text class="f_28 cl_999CA3">删除</text>
                    </view>
                </view>
            </view>
        </view>
		<view class="back_fff br_16 p_32 addressBox" v-for="item in list"  v-if="type==1" @click="SelectAddress(item)">
		    <view>
		        <view class="f_28">
		            {{ item.real_name }}
		            <text class="cl_999CA3 ml_16">{{ item.phone }}</text>
		        </view>
		        <view class="f_28">{{ item.province }}{{ item.city }}{{ item.district }}{{ item.detail }}</view>
		    </view>
		    <view class="flex justify_content_between mt_74">
		        <view>
		            <u-radio-group v-model="defaultItem">
		                <u-radio :customStyle="{ marginBottom: '8px' }" value="设为默认" :disabled="false" label="设为默认" :name="item.id" @change="radioChange"></u-radio>
		                <!-- <u-radio :customStyle="{ marginBottom: '8px' }" v-for="(item, index) in radiolist1" :key="index" :label="item.name" :name="item.name" @change="radioChange"> </u-radio> -->
		            </u-radio-group>
		        </view>
		        <view class="flex">
		            <view class="mr_24" @click="edit(item)">
		                <image class="addicon img_32" src="@/static/imgs/icon_bj.png" mode=""></image>
		                <text class="f_28 cl_999CA3">编辑</text>
		            </view>
		            <view @click="handleRemoveAddress(item.id)">
		                <image class="addicon img_32" src="@/static/imgs/icon_sc.png" mode=""></image>
		                <text class="f_28 cl_999CA3">删除</text>
		            </view>
		        </view>
		    </view>
		</view>
        <view style="height: 100px"></view>
        <view class="addBtn back_3377FF br_24 cl_fff" @click="add" >添加收获地址</view>
    </view>
</template>

<script>
import { defaultAddress, memberInfo, removeAddress } from '@/api';
export default {
    data() {
        return {
            list: [],
			defaultItem:0,
			type:0,
        };
    },
    onShow() {
        uni.$lz.vuex('$current_address', {
            id: '',
            name: '',
            mobile: '',
            latitude: '',
            longitude: '',
            address: '',
            fullAddress: '',
            regionCode: [],
            postcode: '',
            regions: [],
            fullRegion: '',
            isDefault: false
        });
        this.getList();
    },
    // computed: {
    //     defaultItem() {
    //         console.log(this.list);
    //         if (!this.list) {
    //             return '';
    //         }
    //         const defaultItem = this.list?.find((item) => item?.isDefault === true);
    //         console.log(defaultItem);
    //         return defaultItem?.id ?? '';
    //     }
    // },
	onLoad(e) {
		if(e.type){
			this.type=e.type
		}
	},
    methods: {
        edit(item) {
            uni.navigateTo({
                url: '/pages/me/addAddress?type=1&id='+item.id
            });
        },
        add() {
            uni.navigateTo({
                url: '/pages/me/addAddress'
            });
        },
        getList() {
           
				this.$.ajax(1, "POST", "user.address/address_list", {
				}, (res) => {
					if(res.code==200){
						this.list = res.data
						this.defaultItem=res.id
					}else{
						this.$.ti_shi(res.msg)
					}
				})

           
        },
        handleRemoveAddress(id) {
           this.$.ajax(1, "POST", "user.address/address_delete", {
			   id:id
           }, (res) => {
           	if(res.code==200){
           		this.$.ti_shi(res.msg)
           		setTimeout(() => {
           			 this.getList();
           		}, 1000);
           	}else{
           		this.$.ti_shi(res.msg)
           	}
           })
        },
        radioChange(n) {
			
           this.$.ajax(1, "POST", "user.address/edit", {
           			   is_default:1,
					   id:n,
           }, (res) => {
           	if(res.code==200){
           				this.$.ti_shi(res.msg)
           				setTimeout(() => {
           					 this.getList();
           				}, 1000);
           	}else{
           		this.$.ti_shi(res.msg)
           	}
           })
        },
        SelectAddress(item) {
			this.$.set_data("current_address", item)
            // this.$lz.vuex('$current_address', item);
            uni.navigateBack({
				delta:1
			});
        }
    }
};
</script>

<style scoped lang="scss">
.addressBox {
    margin: 8px 16px;
    .addicon {
        vertical-align: middle;
    }
}
.addBtn {
    width: 686rpx;
    height: 100rpx;
    line-height: 100rpx;
    position: fixed;
    bottom: 164rpx;
    left: 50%;
    margin-left: -343rpx;
    text-align: center;
}
</style>
